package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.common.Constants;
import com.bdqn.common.Page;
import com.bdqn.pojo.AppCategory;
import com.bdqn.pojo.AppInfo;
import com.bdqn.pojo.DataDictionary;
import com.bdqn.pojo.DevUser;
import com.bdqn.service.AppCategoryService;
import com.bdqn.service.AppInfoService;
import com.bdqn.service.DataDictionaryService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * DevAppInfoController
 *
 * @author LILIBO
 * @since 2022-01-10
 */
@Controller
@RequestMapping("/dev/app")
public class DevAppInfoController {

    private Logger logger = Logger.getLogger(DevAppInfoController.class);

    @Resource
    private DataDictionaryService dataDictionaryService; // 数据字典服务层

    @Resource
    private AppCategoryService appCategoryService; // 应用分类服务层

    @Resource
    private AppInfoService appInfoService; // 手游应用服务层

    /**
     * 分页查询手游应用信息
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, HttpSession session, @RequestParam(value = "querySoftwareName", required = false) String querySoftwareName, @RequestParam(value = "queryStatus", required = false) Long queryStatus, @RequestParam(value = "queryFlatformId", required = false) Long queryFlatformId, @RequestParam(value = "queryCategoryLevel1", required = false) Long queryCategoryLevel1, @RequestParam(value = "queryCategoryLevel2", required = false) Long queryCategoryLevel2, @RequestParam(value = "queryCategoryLevel3", required = false) Long queryCategoryLevel3, @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex) {
        // 获取当前登录用户（查询该用户的手游应用）
        DevUser devUser = (DevUser) session.getAttribute(Constants.DEV_USER_SESSION);

        // 1、数据回显
        model.addAttribute("querySoftwareName", querySoftwareName);
        model.addAttribute("queryStatus", queryStatus);
        model.addAttribute("queryFlatformId", queryFlatformId);
        model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
        model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
        model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);

        // 2、查询页面所需数据并设置到作用域

        // 查询手游状态下拉框数据
        List<DataDictionary> statusList = dataDictionaryService.getList(Constants.APP_STATUS);
        model.addAttribute("statusList", statusList);

        // 查询所属平台下拉框数据
        List<DataDictionary> flatFormList = dataDictionaryService.getList(Constants.APP_FLATFORM);
        model.addAttribute("flatFormList", flatFormList);

        // 查询一级分类下拉框数据
        List<AppCategory> categoryLevel1List = appCategoryService.getListByParentId(0L);
        model.addAttribute("categoryLevel1List", categoryLevel1List);
        // 查询二级分类下拉框数据
        if (queryCategoryLevel1 != null) {
            List<AppCategory> categoryLevel2List = appCategoryService.getListByParentId(queryCategoryLevel1);
            model.addAttribute("categoryLevel2List", categoryLevel2List);
        }
        // 查询三级分类下拉框数据
        if (queryCategoryLevel2 != null) {
            List<AppCategory> categoryLevel3List = appCategoryService.getListByParentId(queryCategoryLevel2);
            model.addAttribute("categoryLevel3List", categoryLevel3List);
        }

        // 分页查询手游应用列表
        AppInfo queryAppInfo = new AppInfo(); // 构造查询条件对象
        queryAppInfo.setSoftwareName(querySoftwareName);
        queryAppInfo.setStatus(queryStatus);
        queryAppInfo.setFlatformId(queryFlatformId);
        queryAppInfo.setCategoryLevel1(queryCategoryLevel1);
        queryAppInfo.setCategoryLevel2(queryCategoryLevel2);
        queryAppInfo.setCategoryLevel3(queryCategoryLevel3);
        queryAppInfo.setDevId(devUser.getId()); // 设置当前登录者用户ID

        Page pages = new Page(); // 构造分页对象
        int total = appInfoService.getTotal(queryAppInfo); // 查询对应记录总数
        pages.setTotalCount(total); // 设置总记录数
        pages.setCurrPageNo(pageIndex); // 设置要查询第几页

        List<AppInfo> appInfoList = appInfoService.getPage(queryAppInfo, pages);
        model.addAttribute("appInfoList", appInfoList);
        model.addAttribute("pages", pages);

        // 3、跳转到对应页面
        return "developer/appinfolist";
    }

    /**
     * 根据parentId查询相应的分类级别列表
     *
     * @param pid
     * @return
     */
    @ResponseBody
    @GetMapping("/categorylevellist")
    public String getAppCategoryList(@RequestParam(value = "pid", defaultValue = "0") Long pid){
        return JSON.toJSONString(appCategoryService.getListByParentId(pid));
    }
}
